﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class DataReaderUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static T ReadObject<T>(IDataReader reader)
        {
            T obj = (T) Activator.CreateInstance(typeof (T));
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                while (reader.Read())
                {
                    for(int i=0;i<reader.FieldCount;i++)
                    {
                        string colName = reader.GetName(i);
                        if(reader[i]!=DBNull.Value)
                        {
                            foreach(PropertyInfo prop in props)
                            {
                                if(prop.Name.ToLower()==colName.ToLower())
                                {
                                    prop.SetValue(obj, reader[i], null);
                                    break;
                                }
                            }
                        }
                    }
                }
                reader.Close();
            }
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="propColumnNameMappings">from property name to column name</param>
        /// <returns></returns>
        public static T ReadObject<T>(IDataReader reader, Dictionary<string,string> propColumnNameMappings)
        {
            T obj = (T)Activator.CreateInstance(typeof(T));
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                Dictionary<string,object> fieldValues=new Dictionary<string, object>();
                if (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string colName = reader.GetName(i);
                        if (reader[i] != DBNull.Value)
                        {
                            fieldValues.Add(colName.ToLower(), reader[i]);
                        }
                    }
                }
                reader.Close();

                foreach(PropertyInfo prop in props)
                {
                    if(propColumnNameMappings.ContainsKey(prop.Name))
                    {
                        string colName = propColumnNameMappings[prop.Name].ToLower();
                        if(fieldValues.ContainsKey(colName))
                        {
                            prop.SetValue(obj, fieldValues[colName],null);
                        }
                    }
                }
            }
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="propColumnNameMappings">map prop name to column name</param>
        /// <param name="converters">map prop name to type converter</param>
        /// <returns></returns>
        public static T ReadObject<T>(IDataReader reader, Dictionary<string,string> propColumnNameMappings, Dictionary<string,TypeConverter> converters)
        {
            T obj = (T)Activator.CreateInstance(typeof(T));
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                Dictionary<string, object> fieldValues = new Dictionary<string, object>();
                if (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string colName = reader.GetName(i);
                        if (reader[i] != DBNull.Value)
                        {
                            fieldValues.Add(colName.ToLower(), reader[i]);
                        }
                    }
                }
                reader.Close();

                foreach (PropertyInfo prop in props)
                {
                    if (propColumnNameMappings.ContainsKey(prop.Name))
                    {
                        string colName = propColumnNameMappings[prop.Name].ToLower();
                        if (fieldValues.ContainsKey(colName))
                        {
                            object fieldValue = fieldValues[colName];
                            if(converters.ContainsKey(prop.Name))
                            {
                                TypeConverter converter = converters[prop.Name];
                                if(converter.CanConvertFrom(fieldValue.GetType()) && 
                                    converter.CanConvertTo(prop.PropertyType))
                                {
                                    fieldValue = converter.ConvertFrom(null, CultureInfo.CurrentUICulture, fieldValue);
                                }
                            }
                            if (fieldValue.GetType() == prop.PropertyType)
                                prop.SetValue(obj, fieldValues[colName], null);
                        }
                    }
                }
            }
            return obj;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static List<T> ReadObjects<T>(IDataReader reader)
        {
            List<T> objs=new List<T>();
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                while (reader.Read())
                {
                    T obj =(T) Activator.CreateInstance(typeof(T));
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string colName = reader.GetName(i);
                        if (reader[i] != DBNull.Value)
                        {
                            foreach (PropertyInfo prop in props)
                            {
                                if (prop.Name.ToLower() == colName.ToLower())
                                {
                                    prop.SetValue(obj, reader[i], null);
                                    break;
                                }
                            }
                        }
                    }
                    objs.Add(obj);
                }
                reader.Close();
            }
            return objs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="propColumnNameMappings">from property name to column name</param>
        /// <returns></returns>
        public static List<T> ReadObjects<T>(IDataReader reader, Dictionary<string, string> propColumnNameMappings)
        {
            List<T> objs=new List<T>();
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                while (reader.Read())
                {
                    T obj = (T)Activator.CreateInstance(typeof(T));
                    Dictionary<string, object> fieldValues = new Dictionary<string, object>();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string colName = reader.GetName(i);
                        if (reader[i] != DBNull.Value)
                        {
                            fieldValues.Add(colName.ToLower(), reader[i]);
                        }
                    }

                    foreach (PropertyInfo prop in props)
                    {
                        if (propColumnNameMappings.ContainsKey(prop.Name))
                        {
                            string colName = propColumnNameMappings[prop.Name].ToLower();
                            if (fieldValues.ContainsKey(colName))
                            {
                                prop.SetValue(obj, fieldValues[colName], null);
                            }
                        }
                    }
                    objs.Add(obj);
                }
                reader.Close();
            }
            return objs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="propColumnNameMappings">map prop name to column name</param>
        /// <param name="converters">map prop name to type converter</param>
        /// <returns></returns>
        public static List<T> ReadObjects<T>(IDataReader reader, 
            Dictionary<string, string> propColumnNameMappings, 
            Dictionary<string, TypeConverter> converters)
        {
            List<T> objs=new List<T>();
            PropertyInfo[] props = typeof(T).GetProperties();
            if (reader != null)
            {
                
                while (reader.Read())
                {
                    T obj = (T)Activator.CreateInstance(typeof(T));
                    Dictionary<string, object> fieldValues = new Dictionary<string, object>();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string colName = reader.GetName(i);
                        if (reader[i] != DBNull.Value)
                        {
                            fieldValues.Add(colName.ToLower(), reader[i]);
                        }
                    }

                    foreach (PropertyInfo prop in props)
                    {
                        if (propColumnNameMappings.ContainsKey(prop.Name))
                        {
                            string colName = propColumnNameMappings[prop.Name].ToLower();
                            if (fieldValues.ContainsKey(colName))
                            {
                                object fieldValue = fieldValues[colName];
                                if (converters.ContainsKey(prop.Name))
                                {
                                    TypeConverter converter = converters[prop.Name];
                                    if (converter.CanConvertFrom(fieldValue.GetType()) &&
                                        converter.CanConvertTo(prop.PropertyType))
                                    {
                                        fieldValue = converter.ConvertFrom(null, CultureInfo.CurrentUICulture, fieldValue);
                                    }
                                }
                                if (fieldValue.GetType() == prop.PropertyType)
                                    prop.SetValue(obj, fieldValues[colName], null);
                            }
                        }
                    }

                    objs.Add(obj);
                }
                reader.Close();
            }
            return objs;
        }

    }
}
